﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Meta.Quote.Data;

namespace AAA.Quote
{
    public abstract class AbstractHistoryQuote : IHistoryQuote
    {
        private Dictionary<string, DateTime> _dicSymbolLastTime;
        private Dictionary<string, List<BarData>> _dicHistoryData;

        public AbstractHistoryQuote()
        {
            _dicHistoryData = new Dictionary<string, List<BarData>>();
            _dicSymbolLastTime = new Dictionary<string, DateTime>();
        }

        protected void AddBarData(string strSymbolId, List<BarData> lstBarData)
        {
            foreach (BarData barData in lstBarData)
                AddBarData(strSymbolId, barData);
        }

        protected void AddBarData(string strSymbolId, BarData barData)
        {
            List<BarData> lstBarData;
            DateTime dtLastDateTime = DateTime.MaxValue;

            if (_dicHistoryData.ContainsKey(strSymbolId) == false)
            {
                lstBarData = new List<BarData>();
                dtLastDateTime = barData.BarDateTime;
                _dicHistoryData.Add(strSymbolId, lstBarData);
                _dicSymbolLastTime.Add(strSymbolId, barData.BarDateTime);
            }
            else
            {
                lstBarData = _dicHistoryData[strSymbolId];
                dtLastDateTime = _dicSymbolLastTime[strSymbolId];
            }

            if (barData.BarDateTime.CompareTo(dtLastDateTime) < 0)
                return;

            lstBarData.Add(barData);
            _dicSymbolLastTime[strSymbolId] = barData.BarDateTime;
        }

        #region IHistoryQuote Members

        public abstract void InitProgram(Dictionary<string, object> dicParam);

        public abstract void Close();

        public abstract List<BarRecord> GetBarData(string strSymbolId, DateTime dtStartTime, DateTime dtEndTime);

        public abstract List<BarRecord> GetBarData(string strSymbolId);

        public abstract Dictionary<string, List<AAA.Meta.Quote.Data.BarRecord>> GetBarRecord(string strRecordType, string strSymbolId, DateTime dtStartTime, DateTime dtEndTime);

        public abstract Dictionary<string, List<AAA.Meta.Quote.Data.BarRecord>> GetBarRecord(string strRecordType, string strSymbolId);

        #endregion
    }
}
